<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>扩展运算符</title>
	</head>
	<body>
		<script>
		//扩展运算符 “ ... ”
			//作用
			//1.复制
			//2.合并
			
			//1.复制:
			console.log("=============引用=============")
			const a = [1, 2, 3]
		
			const b = a
			a.push(4)
			console.log(b)//打印[1, 2, 3, 4],发现 b 受影响了
			//可以得出const b = a并非 复制/拷贝，而是引用
			//const b = a 是将 a 的地址给 b 
			//对于数组和对象他们变量名里面存的是一个地址，而这个地址里面存的就是 数组中的元素/对象中的属性
			console.log("==============复制==============")
			//真正的复制
				//数组
				const a1 = [11, 22, 33]
				const b1 = { ...a1 }
				
				console.log(b1)
				
				a1.push(44)
				
				console.log(b1)//此时b1就不会受到影响了
			
				//对象
				const obj = {
					id:10002,
					name:'xiaoming',
					age:16,
					address:'XX省XX市'
				}
				const objCopy = { ...obj }
				
				console.log(objCopy)
				
				obj.profession = "doctor"//添加属性
				
				console.log(objCopy)//此时objCopy就不会受到影响了
			
			
			console.log("==============合并==============")
			
			//1.数组合并
			const arr1 = [1, 3, 5, 7]
			const arr2 = [2, 4, 6, 8]
			
			const combineArr = [...arr1, ...arr2]
			console.log(combineArr)
			
			//对象合并
			const obj1 = {
				name: 'zhangshan',
				height: 170
			}
			const obj2 = {
				height: 180,
				age: 16
			}
			
			const combineObj = { ...obj1, ...obj2 }
			//两个对象合并时，若两个对象有相同的属性，则前面那个对象的属性会被覆盖
			//如：obj1的height就被覆盖了
			console.log(combineObj)
			
		</script>
	</body>
</html>